﻿$(document).ready(function () {
    bindEditPage();
    bindListPage();
    //initTooltip();
});

function bindListPage() {
    $("#select_all").click(function () {
        $("input[name=ids]'").attr("checked", $("#select_all").attr("checked"));
    });
    $("a.delete").click(function () {
        var ids = "";
        $("input[name=ids]'").each(function () {
            if ($(this).attr("checked")) {
                ids += $(this).val() + ",";
            }
        });
        if (ids == "") {
            alert("请选择要删除的记录");
        }
        else {
            var url = $(this).attr("rel");
            if (url.indexOf("?") >=0 )
            {
                DoPost(url, "&ids=" + ids);
            }
            else 
            {
                DoPost(url, "?ids=" + ids);    
            }
        }
    });

    $("select.numPerPage").change(function () {
        document.cookie = "numPerPage=" + $(this).val();
        window.location.reload();
    });
}

function bindEditPage() {
    $("a.back").click(function () {
        history.back();
    });

    var eventName = "blur";
    if ($.browser.msie) {
        eventName = "focusout";
    }

    $("#editForm input.required").bind(eventName, function () {
        validate(this);
    });
    $("#editForm textarea.required").bind(eventName, function () {
        validate(this);
    });
    $("#ConfirmPassword").bind(eventName, function () {
        checkPassword();
    });
    $("#editForm").bind("submit", function () {
        //        clearDefaultText();
        var success = true;
        $("#editForm input.required").each(function () {
            success = validate(this) && success;
        });
        $("#editForm textarea.required").each(function () {
            success = validate(this) && success;
        });
        if (!success) {
            alert("请正确填写表单！错误的内容以红色标出！");
        }
        return success;
    });
}

function validate(element) {
    var messageSpan = $("span[targetId=" + $(element).attr("id") + "]");
    isErrorBefore = messageSpan.hasClass("warnMsg");
    messageSpan.removeClass("tipMsg");
    messageSpan.removeClass("successMsg");
    messageSpan.removeClass("warnMsg");
    var success = false;
    if ($(element).hasClass("required")) {
        success = !!$(element).val();
    }
    if ($(element).attr("id") == "ConfirmPassword") {
        success =  success && ($(element).val() == $("#Password").val());
    }
    if ($(element).hasClass("char")) { // 字母、数字、下划线等普通字符的组合
        success = success && isChar($(element).val());
    }
    if ($(element).hasClass("number")) {
        var number = parseInt($(element).val());
        success = success && !isNaN(number);
        if ($(element).attr("max")) {
            success = success && number <= parseInt($(element).attr("max"));
        }
        if ($(element).attr("min")) {
            success = success && number >= parseInt($(element).attr("min"));
        }
    }
    if ($(element).hasClass("email")) {
        success = success && isEmail($(element).val());
    }
    if ($(element).hasClass("mobile")) {
        success = success && isMobile($(element).val());
    }
    if ($(element).hasClass("length")) {
        success = success && $(element).val().length >= parseInt($(element).attr("minlength"));
    }

    // messageSpan.parent().removeClass("warnBlock");
    if (!$(element).attr("tipMsg")) {
        if (!success) {
            messageSpan.addClass("warnMsg");
            // messageSpan.parent().addClass("warnBlock");
            return false;
        }
        else {
            if (isErrorBefore) {
                messageSpan.addClass("successMsg");
                messageSpan.fadeOut(2000);
            }
            else {
                messageSpan.addClass("tipMsg");
            }
            return true;
        }
    }
    else {
        // efe only
        if (!success) {
            $(element).addClass("errorInput");
            $(element).attr("title", $(element).attr("tipMsg"));
            return false;
        }
        else {
            $(element).removeClass("errorInput");
            $(element).attr("title", "");
            return true;
        }
    }
}

// 判断是否是字母、数字、下划线等普通字符的组合
function isChar(str) {
    var reg = new RegExp(/^\w+$/);
    return reg.test(str);
}

// 判断是否是字母、数字、下划线、汉字等的组合
function isCharCn(str) {
    var reg = new RegExp(/^[a-zA-Z0-9\u4e00-\u9fa5]+$/);
    return reg.test(str);
}

function isEmail(email) {
    var reg = new RegExp(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/);
    //var reg = new RegExp(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/);
    return reg.test(email);
}

function isMobile(mobile) {
    var regu = /^[1][3,4,5,8][0-9]{9}$/;
    var re = new RegExp(regu);
    return re.test(mobile);
}



function DoPost(url, param) {
    $.ajax({
        type: 'POST',
        url: url + param,
        dataType: "json",
        cache: false,
        success: ajaxSuccess,
        error: ajaxError
    });
}

function ajaxSuccess(data) {
    if (data.message) {
        alert(data.message);
    }
    if (data.action == 'reload') {
        // window.location.replace(window.location);
        window.location.reload();
    }
    else {
        window.alert(data.message);
    }
}

function ajaxError(XMLHttpRequest, textStatus, errorThrown) {
    alert(XMLHttpRequest.responseText);
    //alert("Failed due to unknown reason, please try later.");
}